<template>
  <div class="checkout-address">
    <div class="text">
      <div v-if="!showAdress" class="none">您需要先添加收货地址才可提交订单。</div>
      <ul v-if="showAdress">
        <li>
          <span>收<i />货<i />人：</span>{{ showAdress.receiver }}
        </li>
        <li><span>联系方式：</span>{{ showAdress.contact.replace(/^(\d{3})\d*(\d{4})$/, '$1****$2') }}</li>
        <li><span>收货地址：</span>{{ showAdress.fullLocation }}{{ showAdress.address }}</li>
      </ul>
      <a @click="AddressEditOpen(showAdress)" href="javascript:;">修改地址</a>
    </div>
    <div class="action">
      <XtxButton  @click="openDaklog" class="btn">切换地址</XtxButton>
      <XtxButton @click="AddressEditOpen({})" class="btn">添加地址</XtxButton>
    </div>
      <!-- 选择地址 对话框 -->
  <XtxDialog title="标题" v-model:visible='visibleDailog' >
         <div :class="`text item ${selectAddress&&selectAddress.id===item.id?'active':''}`" 
         @click="changeSelectAddress(item)"
         v-for="item in list" :key="item.id">
          <ul>
            <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
            <li><span>联系方式：</span>{{item.contact.replace(/^(\d{3})\d*(\d{4})$/, '$1****$2')}}</li>
            <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
          </ul>
        </div>
      <template #footer>
        <XtxButton @click="visibleDailog = false" type="gray" style="margin-right:20px">取消</XtxButton>
        <XtxButton  @click="visibleDailog = false" type="primary">确认</XtxButton>
      </template>
  </XtxDialog>
  <!-- 添加 修改 地址 -->
  <AddressEdit @on-success='successAddAddress' ref="AddressEditDom" />
  </div>
</template>
<script>
import {ref} from 'vue'
import AddressEdit from './address-edit.vue'

export default {
  name: "CheckoutAddress",
  components:{AddressEdit},
  props: {
    // 收货地址列表
    list: {
      type: Array,
      default: [],
    },
  },
  setup(props, { emit }) {
    // 1. 找到默认收货地址
    // 2. 没有默认收货地址，使用第一条收货地址
    // 3. 如果没有数据，提示添加
    const showAdress = ref(false);
    const defaultAddress = props.list.find((item) => item.isDefault === 0);
    if (defaultAddress) {
      showAdress.value = defaultAddress;
    } else {
      if (props.list.length) {
        // 加上一个注释：忽略下面代码的检查eslint
        // eslint-disable-next-line vue/no-setup-props-destructure
        showAdress.value = props.list[0];
      }else{
        // test 数据
         showAdress.value = {contact:'',ullLocation:""}
      }
    }

    // 通知父组件一个收货地址id  没有就null 有就 id
    emit('change',  showAdress.value && showAdress.value.id )

    // dailog的 开关
    const visibleDailog = ref(false)

    // 默认|点击的  地址ID
    const selectAddress = ref(null)
    // 打开对话框
    const openDaklog = () => {
      selectAddress.value = null
      visibleDailog.value = true
    }
    // 修改收货地址
    const changeSelectAddress = (item) => {
      selectAddress.value = item
      showAdress.value = item
      // selectAddress.value?.id)  ===  selectAddress.value && selectAddress.value.id)
      emit('change', selectAddress.value?.id)
    }

    // AddressEdit 组件实例对象
    const AddressEditDom = ref(null)
    const AddressEditOpen = (address) => {
      AddressEditDom.value.open(address)
    }

    // 成功添加地址
    const successAddAddress = (form) => {
      // 根据formData中的ID去当前地址列表中查找，有就是修改，否则是添加
      const address = props.list.find(item => item.id === form.id)
      if (address) {
        for (const key in address) {
          address[key] = form[key]
        }
       }else{
        // 如果是添加：往list中追加一条
        // 当你在修改formData的时候，数组中的数据也会更新，因为是同一引用地址。
        // 啥时候修改formData，当你打开对话框需要清空之前的输入信息
        // 克隆formData数据
        const jsonStr = JSON.stringify(form)
        props.list.unshift(JSON.parse(jsonStr))
       }
    }
    // 成功修改地址
    return { 
      showAdress ,visibleDailog, selectAddress,
       changeSelectAddress, openDaklog, AddressEditDom, 
       AddressEditDom, AddressEditOpen, successAddAddress};
  },
};
</script>
<style scoped lang="less">

.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}

.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
</style>